#define _ALARM_C_

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <string.h>

#include "config.h"
#include "typedefs.h"
#include "alarm.h"
#include "timebase.h"
#include "sensors.h"

//
t_alarm alarm[C_MAX_ALARMS];

extern t_appSettings appSettings;

void alarmBG(void)
{
    static unsigned char ucNextCheckSecond = 1;

// check systemTime = 0:00 -> activate all inactive alarms
    if (systemTime.hour == 0 && systemTime.minute == 0 && systemTime.second == 0) {
        for (unsigned char i = 0; i < C_MAX_ALARMS; i++) {
            if ((alarm[i].ucState & 0x0F) == C_STATE_ALARM_INACTIVE) {
                alarm[i].ucState = C_STATE_ALARM_ACTIVE;
            }
        }
    }

// Check all alarms once per second
    if (systemTime.second == ucNextCheckSecond) {

        for (unsigned char i = 0; i < C_MAX_ALARMS; i++) {
            if ((alarm[i].ucState & 0x0F) == C_STATE_ALARM_ACTIVE) {
                // check for alarm
                if (timer_checkAlarm(&alarm[i].time) == 1) {
                    switch (alarm[i].ucActionType) {
                    case C_ALARM_TYPE_STOP_HEATER:
                        // stop heating controller
                        //stopHeating();

                        break;


                    }
                    alarm[i].ucState = C_STATE_ALARM_TRIGGERED | (alarm[i].ucState & 0xF0);
                }
                // forecasting alarms
                if (alarm[i].ucActionType == C_ALARM_TYPE_START_HEATER){
                    unsigned long ulT1 = systemTime.hour * 3600 + systemTime.minute*60 + systemTime.second;
                    unsigned long ulT2 = alarm[i].time.hour * 3600 + alarm[i].time.hour * 60 + alarm[i].time.second;
                    unsigned int heatingTime = calcHeatingTime(appSettings.siCtrlTargetTemp);
                    ulT1 += heatingTime;
                    if (ulT1 >= ulT2 ){
                        //startHeating();
                        alarm[i].ucState = C_STATE_ALARM_TRIGGERED | (alarm[i].ucState & 0xF0);
                    }
                }
                // reactivate if necessary
                if ((alarm[i].ucState & 0x0F) == C_STATE_ALARM_TRIGGERED){
                    if ((alarm[i].ucState & 0xF0) == C_STATE_ALARM_REPEAT) {
                        alarm[i].ucState = C_STATE_ALARM_INACTIVE | C_STATE_ALARM_REPEAT;
                    } else alarm[i].ucState = C_STATE_ALARM_STOPPED;
                }
            }
        }



        // check alarms next second
        if (systemTime.second == 59) ucNextCheckSecond = 0;
        else ucNextCheckSecond++;
    }

}

unsigned int setAlarm(unsigned char ucID, t_time* time, unsigned char ucAlarmType, unsigned char ucRepeat)
{
    if (ucID > C_MAX_ALARMS) return 1; // Failed

    memcpy(&alarm[ucID].time, time, sizeof(t_time));
    alarm[ucID].ucActionType = ucAlarmType;
    if (timer_checkAlarm(time) == 1) alarm[ucID].ucActionType = C_STATE_ALARM_ACTIVE;
    else alarm[ucID].ucActionType = C_STATE_ALARM_INACTIVE;

    alarm[ucID].ucActionType |= ucRepeat*C_STATE_ALARM_REPEAT;

    return 0; // success
}
